<?php
require_once "BaseAction.php";
class Event_MyController extends TTEvent_Base_Action 
{

	public $visited_user;
	public $owner_user;
	public $is_owner = true;
	
	public function init(){
		parent::init();
		$this->_helper->layout->setLayout('event');
		$this->visited_user = $this->getUser();
	}
	public function indexAction() 
	{
		$this->_helper->eventCommon->buildMainMenuBlock(3);
		$uid = $this->_getParam('uid', 0);
		if($uid > 0){
			$this->owner_user = $this->_helper->home->getUserInfo($uid);
			$this->is_owner = ($this->visited_user->uid == $this->owner_user->uid);
		}
		else
			$this->owner_user = $this->getUser();
		if($this->owner_user->uid==0){
			$this->gotoLogin("http://".$_SERVER['HTTP_HOST'].'/event/my');
		}
		$this->setTitle('Sự kiện của '.$this->owner_user->name);
		
		//-----------------
		$this->view->banners = $this->_helper->eventCommon->getBanner('my');
		
		
		$timeIndex = $this->getRequest()->getQuery('s', 'all');
		$page = $this->getRequest()->getQuery('page', 0);
		
		$this->view->idx = 1;
		$this->view->timeIndex = $timeIndex;
		$this->view->url = "/event/my".($this->is_owner?"":"/".$this->owner_user->uid)."?";
		
		
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('my',$this->owner_user->uid, 1,0,$this->is_owner);
		$assign_list = array();
		
		
		$eventList = $this->_helper->event->getUserEvents($this->owner_user->uid, $page, 20, $timeIndex);
		$pageInfo = array();
		$pageInfo['page'] = $page;
		$pageInfo['total_item'] = $this->_helper->event->getUserEventCount($this->owner_user->uid, $timeIndex);
		$pageInfo['total_page'] = ceil($pageInfo['total_item'] / 20);
		$ext_param = "";
		
		
		$assign_list['event_list'] = $eventList;
		$assign_list['action'] = 'my';
		$assign_list['is_owner'] = $this->is_owner;
		$assign_list['timeIndex'] = $timeIndex;
		$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => "s=$timeIndex", "url" => "/event/my/".$this->owner_user->uid));;
		
		$this->view->ext_param = $ext_param;
		$this->view->htmlEventList = $this->view->partial('event-list.phtml', $assign_list);
		$this->view->owner_user = $this->owner_user;
		$this->view->is_owner = $this->is_owner;
		$this->view->visited_user = $this->visited_user;
		
		//$searchCond = $this->makeSearchCond();
		//$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
	}
	
	
	
	public function ownAction() 
	{
		$this->_helper->eventCommon->buildMainMenuBlock(3);
		$uid = $this->_getParam('uid', 0);
		if($uid > 0){
			$this->owner_user = $this->_helper->home->getUserInfo($uid);
			$this->is_owner = ($this->visited_user->uid == $this->owner_user->uid);
		}
		else
			$this->owner_user = $this->getUser();
		if($this->owner_user->uid==0){
			$this->gotoLogin("http://".$_SERVER['HTTP_HOST'].'/event/my');
		}
		$this->setTitle('Sự kiện do '.$this->owner_user->name ." sáng lập");
		
		//-----------------
		$this->view->banners = $this->_helper->eventCommon->getBanner('my');
		
		
		$timeIndex = $this->getRequest()->getQuery('s', 'all');
		$page = $this->getRequest()->getQuery('page', 0);
		
		$this->view->idx = 2;
		$this->view->timeIndex = $timeIndex;
		$this->view->url = "/event/my/own".($this->is_owner?"":"/".$this->owner_user->uid)."?";
		
		
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('my',$this->owner_user->uid, 2,0,$this->is_owner);
		$assign_list = array();
		
		
		$eventList = $this->_helper->event->getUserOwnEvents($this->owner_user->uid, $page, 20, $timeIndex);
		$pageInfo = array();
		$pageInfo['page'] = $page;
		$pageInfo['total_item'] = $this->_helper->event->getUserOwnEventCount($this->owner_user->uid, $timeIndex);
		$pageInfo['total_page'] = ceil($pageInfo['total_item'] / 20);
		$ext_param = "";
		
		
		$assign_list['event_list'] = $eventList;
		$assign_list['action'] = 'my';
		$assign_list['is_owner'] = $this->is_owner;
		$assign_list['timeIndex'] = $timeIndex;
		$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => "s=$timeIndex", "url" => "/event/my/".$this->owner_user->uid));;
		
		$this->view->ext_param = $ext_param;
		$this->view->htmlEventList = $this->view->partial('event-list.phtml', $assign_list);
		$this->view->owner_user = $this->owner_user;
		$this->view->is_owner = $this->is_owner;
		$this->view->visited_user = $this->visited_user;
		
		//$searchCond = $this->makeSearchCond();
		//$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
		$this->render('index');
	}
	
	
	public function friendAction() 
	{
		$this->_helper->eventCommon->buildMainMenuBlock(3);
		$uid = $this->_getParam('uid', 0);
		if($uid > 0){
			$this->owner_user = $this->_helper->home->getUserInfo($uid);
			$this->is_owner = ($this->visited_user->uid == $this->owner_user->uid);
		}
		else
			$this->owner_user = $this->getUser();
		if($this->owner_user->uid==0){
			$this->gotoLogin("http://".$_SERVER['HTTP_HOST'].'/event/my');
		}
		$this->setTitle('Sự kiện của '.$this->owner_user->name);
		
		//-----------------
		$this->view->banners = $this->_helper->eventCommon->getBanner('my');
		$fid = $this->getRequest()->getPost('fid', 0);
		if(!$fid){
			$fid = $this->getRequest()->getQuery('fid', 0);
		}
		
		
		$timeIndex = $this->getRequest()->getQuery('s', 'all');
		$page = $this->getRequest()->getQuery('page', 0);
		
		$this->view->idx = 4;
		$this->view->timeIndex = $timeIndex;
		$this->view->url = "/event/my/friend".($this->is_owner?"":"/".$this->owner_user->uid)."?";
		
		
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('my',$this->owner_user->uid, 4,0,$this->is_owner);
		$assign_list = array();
		
		$friendList = $this->_helper->friend->listFriend($this->owner_user->uid);
		$totalItem = 0;
		
		if($fid > 0){
			$eventList = $this->_helper->event->getUserFriendEvents($this->owner_user->uid, $fid, $page, 20, $timeIndex);
			$totalItem = $this->_helper->event->getUserFriendEventCount($this->owner_user->uid, $fid, $timeIndex);
		}
		else{
			$fids = array();
			for($i=0; $i<count($friendList); $i++){
				$fids[] = $friendList[$i]->friendID;
			}
			
			$eventList = $this->_helper->event->getUserFriendEvents($this->owner_user->uid, $fids, $page, 20, $timeIndex);
			$totalItem = $this->_helper->event->getUserFriendEventCount($this->owner_user->uid, $fids, $timeIndex);
		}
		$pageInfo = array();
		$pageInfo['page'] = $page;
		$pageInfo['total_item'] = $totalItem;
		$pageInfo['total_page'] = ceil($pageInfo['total_item'] / 20);
		
		
		$assign_list['event_list'] = $eventList;
		$ext_param = "fid=$fid";
		
		$assign_list['action'] = 'my';
		$assign_list['is_owner'] = $this->is_owner;
		$assign_list['timeIndex'] = $timeIndex;
		$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => "s=$timeIndex&".$ext_param, "url" => "/event/my/friend".($this->owner_user->uid!=$this->visited_user->uid?"/".$this->owner_user->uid:"")));;
		
		$this->view->fid = $fid;
		$this->view->ext_param = $ext_param;
		$this->view->htmlEventList = $this->view->partial('event-list.phtml', $assign_list);
		$this->view->owner_user = $this->owner_user;
		$this->view->is_owner = $this->is_owner;
		$this->view->visited_user = $this->visited_user;
		$this->view->friend_list = $friendList;
		
	//	$searchCond = $this->makeSearchCond();
	//	$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
		
		$this->render('index');
	}
	
	public function invitedAction() {
		$this->_helper->eventCommon->buildMainMenuBlock(3);
		
		$uid = $this->_getParam('uid', 0);
		if($uid > 0){
			$this->owner_user = $this->_helper->home->getUserInfo($uid);
			$this->is_owner = ($this->visited_user->uid == $this->owner_user->uid);
		}
		else
			$this->owner_user = $this->getUser();
		
		if($this->owner_user->uid==0){
			$this->gotoLogin("http://".$_SERVER['HTTP_HOST'].'/event/my');
		}
		$this->setTitle('Sự kiện được mời của '.$this->owner_user->name);
		
		//-----------------
		
		$this->view->banners = $this->_helper->eventCommon->getBanner('my');
		
		
		$status = $this->getRequest()->getQuery('s', '');
		
		$page = $this->getRequest()->getQuery('page', 0);
		
		$this->view->idx = 3;
		$this->view->timeIndex = "";
		
		
		$this->_helper->eventCommon->buildHtmlEventLeftMenu('my',$this->owner_user->uid, 3,0,$this->is_owner);
		$assign_list = array();
		
		$limit = 20;
		$cache_key = "event_my_invited_".$this->owner_user->uid."_".$page.($status==""?"":"_".$status);
		$cache_key_count = "event_my_invited_count_".$this->owner_user->uid.($status==""?"":"_".$status);
		$totalItem = $this->getCache($cache_key_count);
		
		$myModel = $this->loadModel('My');
		if($totalItem === false){
			$totalItem = $myModel->getCountMyEventInvited($this->owner_user->uid, $status);
			$this->setCache($cache_key_count, $totalItem);
		}
		
		$eventList = $this->getCache($cache_key);
		
		if($eventList ===false){
			$eventList = $myModel->getMyEventInvited($this->owner_user->uid, $status, $page, $totalItem);
			$this->setCache($cache_key, $eventList);
		}
		//var_dump($eventList);exit();
		
		
		$pageInfo = array();
		$pageInfo['page'] = $page;
		$pageInfo['total_item'] = $totalItem;
		$pageInfo['total_page'] = ceil($totalItem/$limit);
		$ext_param = "";
		
		$assign_list['event_list'] = $eventList;
		$assign_list['action'] = 'invited';
		$assign_list['is_owner'] = $this->is_owner;
		
		$assign_list['status'] = $status;
		$assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => "s=$status", "url" => "/event/my/invited"));;
		
		$this->view->ext_param = $ext_param;
		$this->view->htmlEventList = $this->view->partial('event-list.phtml', $assign_list);
		$this->view->url = "/event/my/invited".($this->is_owner?"":"/".$this->owner_user->uid)."?";
		$this->view->owner_user = $this->owner_user;
		$this->view->status = $status;
		$this->view->is_owner = $this->is_owner;
		$this->view->visited_user = $this->visited_user;
		//$searchCond = $this->makeSearchCond();
	//	$this->view->htmlSearchBox = $this->buildSearchBox($searchCond);
		
		$this->render('index');
	}
	
	private function buildSearchBox($searchCond){
		$user = $this->getUser();
		//$networkList = $this->_helper->network->getUserNetworks($user->uid);
		//$networkList = $this->_helper->Network->getAllNetworks();
		$categoryList = $this->_helper->eventCommon->getEventCategories();
		
		return $this->_helper->eventCommon->buildHtmlSearchBox($searchCond, $categoryList);
	}
	
	private function makeSearchCond(){
		$searchCond = array();
		
		$req = $this->getRequest();
		if($req->isPost()){
			$searchCond['keyword'] = $req->getPost('keyword', "");
			$searchCond['nwid'] = (int)$req->getPost('nwid', "");
			$searchCond['cid'] = (int)$req->getPost('cid', "");
			$searchCond['start_time'] = $req->getPost('start_time', "");
			$searchCond['end_time'] = $req->getPost('end_time', "");
		}
		else{
			$searchCond['keyword'] = $req->getQuery('keyword', "");
			$searchCond['nwid'] = $req->getQuery('nwid', 0);
			$searchCond['cid'] = $this->_getParam('cid', 0);
			$searchCond['start_time'] = $req->getQuery('start_time', "");
			$searchCond['end_time'] = $req->getQuery('end_time', "");
		}
		
		return $searchCond;
	}
}

